
package com.shop.cloud.mall.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.mall.common.entity.VipUser;
import com.shop.cloud.mall.api.mapper.VipUserMapper;
import com.shop.cloud.mall.api.service.VipUserService;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 会员用户
 *
 * @author
 * @date 2024-04-02 15:22:47
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements VipUserService {


    @Override
    public VipUser getByUserId(String userId) {
        return this.getOne(Wrappers.<VipUser>lambdaQuery()
                .eq(VipUser::getUserId, ThirdSessionHolder.getUserId())
                .ge(VipUser::getEndTime, LocalDateTime.now()));
    }

    @Override
    @Klock(name = "updateTotalDiscountAmount", keys = {"#userId"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public void updateTotalDiscountAmount(String userId, BigDecimal amount) {
        this.update(new VipUser(), Wrappers.<VipUser>lambdaUpdate()
                .eq(VipUser::getUserId, userId)
                .setSql("total_discount_amount = total_discount_amount + " + amount));
    }
}
